﻿/**
 * Created by Administrator on 2017/8/12.
 */
define(['api'],function (api) {
    var AddVisitRecord = {
        name: 'add-visit-record',
        template: ''
        + '<Modal'
        + '        title="填写最新回访记录"'
        + '        v-model="showSubmitVisitRecord"'
        + '        width="650"'
        + '       v-on:on-cancel="closeAddVisitRecord"'
        //+ '        ok-text="提交最新回访记录"'
        //+ '        loading="modal_loading"'
        //+ '        v-on:on-ok="submitVisitRecord"'
        + '        class-name="vertical-center-modal newVisit  vertical-center-modal">'
        + '    <div class="modal-content add-visit-record">'
        + '        <div class="one-input newPlan-toggle">'
        + '            <strong class="state-input">'
        + '                <i-select v-model="visitRecord.visitType" placeholder="回访类别" title="回访类别">'
        + '                    <i-option v-for="item in visitTypeOption" :value="item" :key="item">'
        + '                        {{ item}}'
        + '                    </i-option>'
        + '                </i-select>'
        + '            </strong>'
        + '        </div>'
        + '        <div class="time-choice">'
        + '            <strong >'
        + '                <Row>'
        + '                    <Col >'
        + '                         <Date-picker type="date" v-bind:placeholder="visitTimeLabel" v-bind:editable="false"  placement="bottom-end" v-bind:title="visitTimeLabel" v-model="visitRecord.visitTime" v-on:on-change="visitTimeChange"></Date-picker>'
        + '                    </Col>'
        + '                </Row>'
        + '            </strong>'
        + '        </div>'
        + '        <div class="one-input">'
        + '            <strong class="state-input">'
        + '                <i-select v-model="visitRecord.visitor" v-bind:placeholder="salespersonLabel" v-bind:title="salespersonLabel" filterable >'
        + '                    <i-option v-for="item in staffOption" :value="item.value" :key="item.label">{{item.label}}</i-option>'
        + '                </i-select>'
        + '            </strong>'
        + '        </div>'
        + '        <div class="one-input" v-show="!isTraded">'
        + '            <strong class="state-input" style="width: 170px">'
        + '                <i-select v-model="visitRecord.isFail" placeholder="接通情况" title="接通情况">'
        + '                    <i-option v-for="item in isFailOption" :value="item.value" :key="item">{{ item.label}}</i-option>'
        + '                </i-select>'
        + '            </strong>'
        + '        </div>'
        + '        <div class="one-input" v-show="isTraded">'
        + '            <strong class="state-input" style="width: 170px">'
        + '                <i-select v-model="visitRecord.tradedHouseId" placeholder="成交房源" title="成交房源">'
        + '                    <i-option v-for="item in houseOption" :value="item.value" :key="item">{{ item.label}}</i-option>'
        + '                </i-select>'
        + '            </strong>'
        + '        </div>'
        + '        <div class="one-input" v-show="!isTraded">'
        + '            <strong class="state-input">'
        + '                <i-select v-model="visitRecord.purchasingIntention" placeholder="购买意向" title="购买意向">'
        + '                    <i-option v-for="item in purchasingIntentionOption" :value="item.value" :key="item">{{ item.label }}</i-option>'
        + '                </i-select>'
        + '            </strong>'
        + '        </div>'
        + '        <!-- 上部分隐藏-->'
        + '        <ul v-show="!isTraded" >'
        + '            <li class="pri-li new_see_record" v-show="visitRecord.visitType===\'带看回访\'">'
        + '                <p class="model-right01 remove-right01" style="width: 490px">'
        + '                    <i-select v-model="visitRecord.lookedHouseIdList"   multiple filterable   placeholder="请选择带看的房源，可多选" title="带看房源">'
        + '                        <i-option v-for="item in houseOption" :value="item.value" :key="item">{{ item.label }}</i-option>'
        + '                    </i-select>'
        + '                </p>'
        + '            </li>'
        + '            <li class="pri-li new_flight_record" v-show="visitRecord.visitType===\'接机回访\'">'
        + '                <input placeholder="请在这里输入接机的航班号" title="航班号" v-model="visitRecord.flight">'
        + '            </li>'
        + '            <li class="pri-li new_flight_record" v-show="visitRecord.visitType===\'电话回访\'">'
        + '                <input placeholder="请在这里输入拨打的电话号码" title="电话号码" v-model="visitRecord.callNo">'
        + '            </li>'
        + '        </ul>'
        + '        <div class="one-input" v-for="item in customProperty" v-show="!isTraded"  style="border-bottom: 0;">'
        + '            <strong class="state-input" v-bind:class="{lastCustomInOneLine:item.isFourth}">'
        + '                <i-select v-model="item.customPropertyValue" v-bind:placeholder="item.customPropertyName" v-bind:title="item.customPropertyName">'
        + '                    <i-option v-for="o in item.customPropertyOption" :value="o.OptionToString" :key="o.OptionToString">{{ o.OptionToString }}</i-option>'
        + '                </i-select>'
        + '            </strong>'
        + '        </div>'
        + '        <p class="model-right01 remove-right01" style="width: 650px" v-show="isTraded">'
        + '            <i-select v-model="visitRecord.helperList"  multiple filterable  placeholder="协助人员" title="协助人员">'
        + '                <i-option v-for="item in staffOption" :value="item.value" :key="item.label">{{item.label}}</i-option>'
        + '            </i-select>'
        + '        </p>'

        + '         <div class="one-input" v-show="isTraded">'
        + '             <strong class="inputNum">'
        + '                 <Input-number v-model="visitRecord.tradedPrice" placeholder="成交价格(人民币)" title="成交价格(人民币)"></Input-number>'
        + '             </strong>'
        + '         </div>'
        + '         <div class="one-input" v-show="isTraded">'
        + '             <strong class="state-input">'
        + '                 <i-select v-model="visitRecord.tradedPayType" placeholder="支付方式"  title="支付方式">'
        + '                     <i-option v-for="item in tradedPayTypeOption" :value="item.value" :key="item">'
        + '                         {{ item.label }}'
        + '                     </i-option>'
        + '                 </i-select>'
        + '             </strong>'
        + '         </div>'
        + '         <div class="one-input" v-show="isTraded">'
        + '             <strong class="state-input" >'
        + '                  <span class="deal-area" style="color: #C5C5C5;"  title="成交房源面积">{{visitRecord.tradedHouseArea}}</span>'
        + '             </strong>'
        + '         </div>'
        + '         <div class="one-input" v-show="isTraded">'
        + '             <strong class="state-input" style="width: 170px">'
        + '                  <span class="deal-area" style="color: #C5C5C5;" title="成交房源户型">{{visitRecord.tradedHouseType}}</span>'
        + '             </strong>'
        + '         </div>'
        + '        <input type="text"  class="inputLine" readonly="true">'//做分隔线用
        + '        <div title="追踪情况" style="height:100px" v-bind:class="{showbottonline:isTraded}" class="quill-editor-div">'
        + '             <quill-editor class="editor-example" ref="myTextEditor" :content="quillEditor.content" :options="quillEditor.editorOption" @change="onEditorChange($event)"> </quill-editor>'
        + '        </div>'

        + '        <div class="jl_box"  v-show="!isTraded" ><span>定制下次回访计划</span></div>'
        + '        <div class="one-input jhhflx" v-show="!isTraded" >'
        + '            <strong class="state-input nextPlan-toggle">'
        + '                <i-select v-model="visitRecord.planType" placeholder="计划回访类别" title="计划回访类别">'
        + '                    <i-option v-for="item in planTypeOption" :value="item" :key="item">{{ item}}</i-option>'
        + '                </i-select>'
        + '            </strong>'
        + '        </div>'
        + '        <div class="time-choice" v-show="!isTraded" >'
        + '            <strong >'
        + '                <Row>'
        + '                    <Col >'
        + '                         <Date-picker type="date" placeholder="计划回访时间" title="计划回访时间" style="width:162px; height:30px;" placement="bottom-end" v-model="visitRecord.planTime" v-on:on-change="planTimeChange"></Date-picker>'
        + '                    </Col>'
        + '                </Row>'
        + '            </strong>'
        + '        </div>'
        + '        <i class="zhanwei" v-show="!isTraded && visitRecord.planType!=\'接机回访\'" ></i>'
        + '        <!-- 下部分隐藏-->'
        + '        <ul v-show="!isTraded" >'
        + '            <li class="pri-li new_see_plan" v-for="item in visitRecord.planLookList" v-show="visitRecord.planType===\'带看回访\'">'
        + '                <p class="model-right01 remove-right01">'
        + '                    <i-select v-model="item.houseId"  style="width:100%" clearable placeholder="请选择计划带看的房源" title="带看房源" v-on:on-change="selectPlanLookHouse">'
        + '                        <i-option v-for="item in houseOption" :value="item.value" :key="item">{{ item.label }}</i-option>'
        + '                    </i-select>'
        + '                </p>'
        + '                <p class="model-right01 remove-right01" style="width: 172px" >'
        + '                    <i-select v-model="item.status" style="width:100%" clearable  placeholder="房源带看状态" title="房源带看状态">'
        + '                        <i-option v-for="item in planLookStatusOption" :value="item" :key="item">{{ item }}</i-option>'
        + '                    </i-select>'
        + '                </p>'
        + '            </li>'
        + '            <li class="pri-li new_flight_plan" v-show="visitRecord.planType===\'接机回访\'">'
        + '                <input placeholder="航班号" title="航班号" v-model="visitRecord.planFlight" type="text">'
        + '            </li>'
        + '        </ul>'

        + '    </div>'

        + '    <div slot="footer">'
        + '        <Button size="large" :loading="modal_loading" v-on:click.stop="submitVisitRecord" class="ivu-btn-primary  submit-visit-record">'
        + '            提交最新回访记录'
        + '        </Button>'
        + '        <Button size="large"  v-on:click="closeAddVisitRecord" class="ivu-btn-text">'
        + '            取消'
        + '        </Button>'
        + '    </div>'
        + '</Modal>',
        props: {
        },
        data: function () {
            return {
                showSubmitVisitRecord: false,
                modal_loading: false,
                visitRecord: {
                    customerId: "",
                    nodeId: "",
                    salespersonId: localStorage.userId,
                    visitor: localStorage.userId,
                    planId: "",
                    visitType: "电话回访",
                    visitTime: (new Date()).getFullYear() + "-" + ((new Date()).getMonth() + 1) + "-" + (new Date()).getDate(),
                    isFail: "",
                    purchasingIntention: 4,
                    customPropertyList: [
                    ],
                    customPropertyValueList: [
                    ],
                    callNo: "",
                    flight: "",
                    lookedHouseIdList: [],
                    remark: "",
                    remarkHtml: "",
                    planType: "",
                    planTime: (new Date()).getFullYear() + "-" + ((new Date()).getMonth() + 1) + "-" + (new Date()).getDate(),
                    planLookList: [
                        {
                            houseId: "",
                            status:""
                        }
                    ],
                    planFlight: "",
                    helperList: [],
                    tradedHouseId: "",
                    tradedPrice: 0,
                    tradedPayType: 0,
                    tradedHouseArea: "成交房源面积",
                    tradedHouseType: "成交房源户型",
                },
                quillEditor: {
                    name: 'base-example',
                    content: '',
                    editorOption: {
                        theme: 'bubble',
                        placeholder: "请输入追踪情况，按“提交回访情况”进行提交",
                        modules: {
                            toolbar: [
                                ['bold', 'italic', 'underline', 'strike'],
                                [{ 'list': 'ordered' }, { 'list': 'bullet' }],
                                [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
                                [{ 'color': [] }, { 'background': [] }],
                                [{ 'font': [] }],
                                [{ 'align': [] }],
                                ['link', 'image'],
                                ['clean']
                            ]
                        }
                    }
                },
                visitTypeOption: ["电话回访", "接机回访", "带看回访", "成交回访"],
                planTypeOption: ["电话回访", "接机回访", "带看回访"],
                planLookStatusOption: ["可随时带看", "已联系业主", "未联系业主"],
                isFailOption: [
                    {
                        label: "回访失败",
                        value: "true"
                    },
                    {
                        label: "回访成功",
                        value: "false"
                    }
                ],
                purchasingIntentionOption: [
                    {
                        value: 0,
                        label: '意向强'
                    },
                    {
                        value: 1,
                        label: '意向一般'
                    },
                    {
                        value: 2,
                        label: '意向弱'
                    },
                    {
                        value: 3,
                        label: '无意向'
                    },
                    {
                        value: 4,
                        label: '未回访'
                    }
                ],
                tradedPayTypeOption: [
                    {
                        value: 0,
                        label: '一次性付清全款'
                    },
                    {
                        value: 1,
                        label: '按揭贷款'
                    },
                    {
                        value: 2,
                        label: '分期付款'
                    }
                ],
                customProperty: [
                //#缺失部分#
                    {
                        customPropertyName: "自定义字段1",
                        customPropertyId: "自定义字段1",
                        customPropertyValue: "",
                        isFourth:false,
                        customPropertyOption: [
                            {
                                label: "选项1",
                                value: "1"
                            },
                            {
                                label: "选项2",
                                value: "2"
                            },
                        ]
                    },
                    //{
                    //    customPropertyName: "自定义字段2",
                    //    customPropertyValue: "",
                    //    customPropertyOption: [
                    //        {
                    //            label: "选项3",
                    //            value: "3"
                    //        },
                    //        {
                    //            label: "选项4",
                    //            value: "4"
                    //        },
                    //    ]
                    //},
                    //{
                    //    customPropertyName: "自定义字段3",
                    //    customPropertyValue: "",
                    //    customPropertyOption: [
                    //        {
                    //            label: "选项5",
                    //            value: "5"
                    //        },
                    //        {
                    //            label: "选项6",
                    //            value: "6"
                    //        },
                    //    ]
                    //}
                ],
                staffOption: [],
                houseOption: [
                //#缺失部分#
                    {
                        label: "房源1",
                        value: "f1"
                    },
                    {
                        label: "房源2",
                        value: "f2"
                    }
                ],
            }
        },
        components: {
        },
        mounted: function () {
            var thisAdd = this;
            //监视showSubmitVisitRecord
            window.bus.$on("showSubmitVisitRecord", function (Id) {
                thisAdd.loadVisitRecord(Id);
            });
            api.Get_Customer_Custom_Property()
                .done(function (data) {
                    if (data.IsSuccess) {
                        thisAdd.customProperty = [];
                        for (var i = 0; i < data.ResultObject.length; i++) {
                            var property = {};
                            property.customPropertyId = data.ResultObject[i].Id;
                            property.customPropertyName = data.ResultObject[i].PropertyName;
                            property.customPropertyValue = "";
                            property.isFourth = (i + 1) % 4 == 0;
                            property.customPropertyOption = data.ResultObject[i].Options;
                            thisAdd.customProperty[i] = property;
                        }
                    } else {
                        thisAdd.$Message.error(data.ErrMsg);
                    }
                })
                .fail(function () {
                    thisAdd.$Message.error('查询出错');
                });
            api.Get_Staff_By_CompanyId({ companyId: localStorage.companyId })
                .done(function (data) {
                    if (data.IsSuccess) {
                        thisAdd.staffOption = data.ResultObject;
                    } else {
                        thisAdd.$Message.error(data.ErrMsg);
                    }
                })
                .fail(function () {
                    thisAdd.$Message.error('查询出错');
                });
        },
        computed: {
            isTraded: function () {
                return this.visitRecord.visitType === "成交回访";
            },
            salespersonLabel: function () {
                return this.visitRecord.visitType === "成交回访" ? "销售员" : "回访人员";
            },
            visitTimeLabel: function () {
                return this.visitRecord.visitType === "成交回访" ? "成交时间" : "回访时间";
            },
            editor: function () {
                return this.$refs.myTextEditor.quill
            }
        },
        methods: {
            loadVisitRecord: function (Id) {
                var thisAdd = this;
                thisAdd.visitRecord.customerId = Id;
                thisAdd.isFail = "";
                api.Get_Visit_Plan({ customerId:Id })
                    .done(function (data) {
                        if (data.IsSuccess) {
                            if (data.ResultObject) {
                                thisAdd.visitRecord.visitType = data.ResultObject.VisitType;
                                thisAdd.visitRecord.planId = data.ResultObject.Id;
                                thisAdd.visitRecord.customerId = data.ResultObject.CustomerId;
                                thisAdd.visitRecord.nodeId = data.ResultObject.NodeId;
                                thisAdd.visitRecord.salespersonId = data.ResultObject.SalespersonId;
                                thisAdd.visitRecord.visitTime = data.ResultObject.PlannedTime;
                                thisAdd.visitRecord.purchasingIntention = data.ResultObject.PurchasingIntention;
                                thisAdd.visitRecord.flight = data.ResultObject.Flight ? data.ResultObject.Flight:"";
                                thisAdd.visitRecord.lookedHouseIdList = data.ResultObject.VisitHouseIdList ? data.ResultObject.VisitHouseIdList:[];
                            }
                            thisAdd.showSubmitVisitRecord = true;
                            //触发deleteFocus
                            window.bus.$emit("deleteFocus");
                        } else {
                            thisAdd.$Message.error(data.ErrMsg);
                        }
                    })
                    .fail(function () {
                        thisAdd.$Message.error('查询出错');
                    });
            },
            visitTimeChange: function (date) {
                this.visitRecord.visitTime = date;
            },
            planTimeChange: function (date) {
                this.visitRecord.planTime = date;
            },
            closeAddVisitRecord: function () {
                this.showSubmitVisitRecord = false;
                //触发addFocus
                window.bus.$emit("addFocus");
            },
            selectPlanLookHouse: function () {
                var tempList = this.visitRecord.planLookList;
                this.visitRecord.planLookList = [];
                for (var i = 0; i < tempList.length; i++) {
                    if (tempList[i].houseId != "") {
                        this.visitRecord.planLookList.push(tempList[i]);
                    }
                }
                var v = {
                    houseId: "",
                    status: ""
                };
                this.visitRecord.planLookList.push(v);
            },
            validateAll: function () {
                var isFail = false;
                isFail = isFail || this.validateIsFail();
                isFail = isFail || this.validatePurchasingIntention();
                isFail = isFail || this.validateCallRecord();
                isFail = isFail || this.validateFlightRecord();
                isFail = isFail || this.validateLookedHouseRecord();
                isFail = isFail || this.validateTradedRecordHouse();
                isFail = isFail || this.validateTradedRecordPrice();
                isFail = isFail || this.validateRemark();
                isFail = isFail || this.validatePlanLookStatus();
                return isFail;
            },
            validateIsFail: function () {
                if (this.visitRecord.visitType != "成交回访" && this.visitRecord.isFail === "") {
                    this.$Message.error("必须选择接通情况");
                    return true;
                }
                else {
                    if (this.visitRecord.visitType === "成交回访")
                    {
                        this.visitRecord.isFail = "false";
                    }
                    return false;
                }
            },
            validatePurchasingIntention: function () {
                if (this.visitRecord.visitType != "成交回访" && this.visitRecord.isFail === "false" && this.visitRecord.purchasingIntention===4) {
                    this.$Message.error("回访成功时，意向度不能为未回访");
                    return true;
                }
                else {
                    return false;
                }
            },
            validatePlanLookStatus: function () {
                for (var i = 0; i < this.visitRecord.planLookList.length; i++) {
                    if (this.visitRecord.visitType != "成交回访" && this.visitRecord.planType === "带看回访"  && this.visitRecord.planLookList[i].houseId != "" && this.visitRecord.planLookList[i].status==="") {
                        this.$Message.error("带看计划中选择带看房源的带看状态都不能为空");
                        return true;
                    }
                    return false;
                }
            },
            validateCallRecord: function () {
                if (this.visitRecord.visitType === "电话回访" && this.visitRecord.isFail === "false" && this.visitRecord.callNo === "") {
                    this.$Message.error("电话回访成功时，电话号码不能为空");
                    return true;
                }
                else {
                    return false;
                }
            },
            validateFlightRecord: function () {
                if (this.visitRecord.visitType === "接机回访" && this.visitRecord.isFail === "false" && this.visitRecord.flight === "") {
                    this.$Message.error("接机成功时，接机航班号不能为空");
                    return true;
                }
                else {
                    return false;
                }
            },
            validateLookedHouseRecord: function () {
                if (this.visitRecord.visitType === "带看回访" && this.visitRecord.isFail === "false" && this.visitRecord.lookedHouseIdList.length === 0) {
                    this.$Message.error("带看成功时，带看房源不能为空");
                    return true;
                }
                else {
                    return false;
                }
            },
            validateTradedRecordHouse: function () {
                if (this.visitRecord.visitType === "成交回访" && this.visitRecord.tradedHouseId === "") {
                    this.$Message.error("成交房源不能为空");
                    return true;
                }
                else {
                    return false;
                }
            },
            validateTradedRecordPrice: function () {
                if (this.visitRecord.visitType === "成交回访" && this.visitRecord.tradedPrice === 0) {
                    this.$Message.error("写成交价格不能为0");
                    return true;
                }
                else {
                    return false;
                }
            },
            validateRemark: function () {
                if (this.visitRecord.remark === "") {
                    this.$Message.error("回访情况不能为空");
                    return true;
                }
                else {
                    return false;
                }
            },
            initializeData: function () {
                this.visitRecord.isFail = "";
                this.visitRecord.remark = "";
            },
            submitVisitRecord: function () {
                var thisAdd = this;
                if (thisAdd.validateAll()) {
                    return;
                } else {
                    thisAdd.modal_loading = true;
                    api.Post_Submit_Visit_Record(thisAdd.visitRecord)
                        .done(function (data) {
                            if (data.IsSuccess) {
                                thisAdd.$Message.success('提交成功');
                                //触发submitVisitRecordSuccess
                                window.bus.$emit("submitVisitRecordSuccess");
                                thisAdd.initializeData();
                                thisAdd.modal_loading = false;
                                thisAdd.showSubmitVisitRecord = false;
                            } else {
                                thisAdd.$Message.error(data.ErrMsg);
                                thisAdd.modal_loading = false;
                            }
                        })
                        .fail(function () {
                            thisAdd.$Message.error('提交失败');
                            thisAdd.modal_loading = false;
                        });
                }
            },
            onEditorChange({ editor, html, text }) {
                // console.log('editor change!', editor, html, text)
                this.visitRecord.remarkHtml = html;
                this.visitRecord.remark = text;
            }
        },
        watch: {
            customProperty: function (newArea) {
                this.visitRecord.customPropertyList = [];
                this.visitRecord.customPropertyValueList = [];
                for (var i = 0; i < newArea.length; i++) {
                    if (newArea[i].customPropertyValue != "") {
                        this.visitRecord.customPropertyList.push(newArea[i].customPropertyId);
                        this.visitRecord.customPropertyValueList.push(newArea[i].customPropertyValue);
                    }
                }
            },
        },
    };
    return AddVisitRecord;
});

